package com.wan37.logic.backpack.item;

import com.wan37.logic.domain.Result;
import com.wan37.logic.handler.server.MpHandler;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.DelayDequeThreadPollUtil;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameBackpack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-06-24 10:28
 **/
@Component("healthPotion")
public class HealthPotionItem implements Item {
    @Autowired
    private DelayDequeThreadPollUtil delayDequeThreadPollUtil;
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private RouteUtils routeUtils;
    @Autowired
    private SceneServiceCache sceneServiceCache;
    @Autowired
    private ItemManager itemManager;

    @Override
    public Result execute(UserServiceCache.User user, RoleServiceCache.Role role, GameBackpack.CGameItemMsg cGameItemMsg) {
        int count = user.getItemVOMap().get(cGameItemMsg.getBackpackId()).getCount();
        if (count >= 0) {
            int time = 10;
            int number = 500;
            int timeCount = number / time;
            for (long i = 0; i < time; i++) {
                delayDequeThreadPollUtil.updateTimeSchedule(i, role.getRoleVO().getAllRoleId(),
                        userServiceCache.getUserServiceMap().get(role.getRoleVO().getUserId()).getSceneId(),
                        new MpHandler((long) timeCount, role.getRoleVO()));
            }
            itemManager.reduceItem(user, user.getItemVOMap().get(cGameItemMsg.getBackpackId()), 1);
            return new Result(CommonBackpack.ITEM_SUCCESS, "生命药水使用成功！！");
        } else {
            return new Result(CommonBackpack.ITEM_ERROR, "生命药水使用失败，原因是物品余额为0！！");
        }
    }
}
